package com.situ.web.servlet;

import com.situ.web.pojo.Teacher;
import com.situ.web.pojo.query.TeacherQuery;
import com.situ.web.service.ITeacherService;
import com.situ.web.service.impl.TeacherServiceImpl;
import com.situ.web.util.JSONUtil;
import com.situ.web.util.PageResult;
import com.situ.web.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/teacher")
public class TeacherServlet extends HttpServlet {
    private ITeacherService teacherService = new TeacherServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }

        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "selectById":
                selectById(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
        }
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        teacherService.deleteAll(ids);
        JSONUtil.toJSON(resp, Result.ok("删除成功"));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String subject = req.getParameter("subject");
        Teacher teacher = new Teacher(Integer.parseInt(id), name, Integer.parseInt(age), gender, subject);


        teacherService.update(teacher);

        JSONUtil.toJSON(resp, Result.ok("编辑成功"));
    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.selectById");
        String id = req.getParameter("id");
        Teacher teacher = teacherService.selectById(Integer.parseInt(id));

        JSONUtil.toJSON(resp, Result.ok(teacher));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.add");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String subject = req.getParameter("subject");
        Teacher teacher = new Teacher(name, Integer.parseInt(age),gender,subject);
        teacherService.add(teacher);

        JSONUtil.toJSON(resp, Result.ok("添加成功"));
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.deleteById");
        String id = req.getParameter("id");
        teacherService.deleteById(Integer.parseInt(id));

        //ajax只需要返回这次请求是成功了还是失败了，具体页面跳转由前端跳转
        //code、msg、data
        Result result = new Result(0, "删除成功");
        JSONUtil.toJSON(resp, result);
        //toJSON(resp, result);
    }

    //http://localhost:8081/course?method=selectByPage&page=1&limit=10
    //http://localhost:8081/course?method=selectByPage&page=1&limit=10&name=Java&credit=3
    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("TeacherServlet.selectByPage");
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String age = req.getParameter("age");
        String gender = req.getParameter("gender");
        String subject = req.getParameter("subject");

        TeacherQuery teacherQuery = new TeacherQuery(Integer.parseInt(page), Integer.parseInt(limit), name,age,gender,subject);
        //PageResult<Course> pageResult = courseService.selectByPage(Integer.parseInt(page), Integer.parseInt(limit), name, credit);
        PageResult<Teacher> pageResult = teacherService.selectByPage(teacherQuery);

        JSONUtil.toJSON(resp, pageResult);
    }
}
